package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpTerminationReason;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpTerminationReasonForm;
import com.edu.hre.struts.form.pim.EmpTerminationReasonListForm;

public class EmpTerminationReasonDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empTerminationReasonDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpTerminationReasonForm> getTerminationReasonListInfo(int limit, int offset){

		List<HreEmpTerminationReason> hreTerminationReasons = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreEmpTerminationReason.class);
		ArrayList<EmpTerminationReasonForm> terminationReasons = new ArrayList<EmpTerminationReasonForm>();
		
		if (hreTerminationReasons == null) {
			return terminationReasons;
		}
		for (HreEmpTerminationReason hreTer: hreTerminationReasons) {
			
			try {

				EmpTerminationReasonForm terminationReason = new EmpTerminationReasonForm();
				terminationReason.setId(hreTer.getId());
				terminationReason.setName(CommonUtils.normalizeString(hreTer.getName()));
	
				terminationReasons.add(terminationReason);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return terminationReasons;
	}
	
	public int countTerminationReasons(){

		int count = baseService.countByColumns(null, null, null, "id", HreEmpTerminationReason.class);
		return count < 0 ? 0 : count;
	}

	public void deleteTerminationReasons(EmpTerminationReasonListForm form){

		ArrayList<EmpTerminationReasonForm> info = (ArrayList<EmpTerminationReasonForm>) form.getAllReasons();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpTerminationReasonForm reason = info.get(i);
				if (reason.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, reason.getId(), null, null, 0, HreEmpTerminationReason.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
